package com.atguigu.yygh.hosp.testmongo;

import com.atguigu.yygh.common.result.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @program: yygh_parent
 * @author: Mr.Zhuo
 * @create: 2022-03-10 22:35
 **/
@Api("测试mongo02")
@RestController
@RequestMapping("/mongo02")
public class TestMongo2 {

    @Autowired
    private UserRepository userRepository;

    @ApiOperation("添加测试")
    @GetMapping("insert")
    public void insert() {
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setEmail("3332200@qq.com");
        userRepository.insert(user);
    }

    @ApiOperation("查询所有测试")
    @GetMapping("findAll")
    public R findAll() {
        List<User> all = userRepository.findAll();

        return R.ok().data("list", all);
    }

    //id查询
    @ApiOperation("根据id查询值")
    @GetMapping("findId/{id}")
    public R getById(@PathVariable String id) {

        User user = userRepository.findById(id).get();

        return R.ok().data("user", user);

    }

    //条件查询
    @ApiOperation("条件查询")
    @GetMapping("findQuery")
    public R findUserList() {
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        Example<User> example = Example.of(user);
        List<User> all = userRepository.findAll(example);

        return R.ok().data("list", all);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public void findUsersLikeName() {
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example<User> userExample = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(userExample);
        System.out.println(userList);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("findPage")
    public void findUsersPage() {
        // 首先进行age排序
        Sort sort = Sort.by(Sort.Direction.DESC, "age");
        // 然后进行数据分页
        PageRequest pageRequest = PageRequest.of(0, 2, sort);

        // 还有数据条件查询
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        User user = new User();
        user.setName("三");
        Example<User> example = Example.of(user, exampleMatcher);

        Page<User> all = userRepository.findAll(example, pageRequest);

        System.out.println(all);
        System.out.println(all.getTotalPages());
        System.out.println(all.getContent());

    }

    //修改
    @ApiOperation("修改")
    @GetMapping("update")
    public void updateUser() {
        User user = userRepository.findById("622a07978f7018a83f49b587").get();

        user.setAge(35);
        user.setName("大傻逼");

        userRepository.save(user);
    }


    //删除
    @ApiOperation("删除")
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("622a07978f7018a83f49b587");
    }


    // spring data框架中提供了一系列方便的方法
    @ApiOperation("根据条件查询data")
    @GetMapping("getByNameAndAge")
    public R getByNameAndAge() {
        List<User> list = userRepository.findByNameAndAge("张三", 20);

        return R.ok().data("list", list);
    }

    // springdata 中实现模糊查询
    @ApiOperation("data中实现模糊查询")
    @GetMapping("getByNameLike")
    public R getByNameLike() {
        List<User> list = userRepository.getByNameLike("三");

        return R.ok().data("list", list);
    }


}
